<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改题目')"/>
    <th:block th:include="include :: summernote-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-question-edit" th:object="${question}">
        <input id="id" name="id" th:field="*{id}" type="hidden">
        <input id="treeId" name="courseId" type="hidden" th:field="*{courseId}"/>
        <div class="form-group">
            <label class="col-sm-3 control-label">所属课程：</label>
            <div class="col-sm-8">
                <div class="input-group">
                    <input class="form-control" type="text" onclick="courseRadioTree()" id="treeName" readonly="true"
                           th:value="${courseName == null ? '无' : courseName}">
                    <span class="input-group-addon"><i class="fa fa-search"></i></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">题型：</label>
            <div class="col-sm-10">
                <select name="questionType" class="form-control m-b"
                        th:with="dicts=${@sysDictDataServiceImpl.listDictDataByTypeSign('tiku_question_type')}">
                    <option th:each="dict : ${dicts}" th:text="${dict.dataKey}" th:value="${dict.dataValue}"
                            th:field="*{questionType}"></option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">题目内容：</label>
            <div class="col-sm-10">
                <input id="fullContent" name="fullContent" type="hidden" th:field="*{fullContent}" required>
                <div id="editor" class="summernote"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label ">知识点：</label>
            <div class="col-sm-6">
                <input id="reviewPoint" name="reviewPoint" th:field="*{reviewPoint}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-2 control-label">编辑辅助工具网站：</label>
            <div class="btn-group" role="group">
                <button type="button" class="btn btn-default" onclick="mathEdit()">数学公式编辑</button>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label is-required">显示顺序：</label>
            <div class="col-sm-8">
                <input class="form-control" type="text" name="orderNum" id="orderNum" th:field="*{orderNum}" required>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">使用状态：</label>
            <div class="col-sm-8">
                <div class="radio-box"
                     th:each="dict : ${@sysDictDataServiceImpl.listDictDataByTypeSign('sys_use_status')}">
                    <input type="radio" th:id="${dict.id}" name="use" th:value="${dict.dataValue}" th:field="*{use}">
                    <label th:for="${dict.id}" th:text="${dict.dataKey}"></label>
                </div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea id="remark" name="remark" class="form-control">[[*{remark}]]</textarea>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: summernote-js"/>
<script type="text/javascript">
    const prefix = ctx + "tiku/question";

    $(function () {
        $('.summernote').summernote({
            placeholder: '请输入题目内容，请注意格式',
            height: 350,
            lang: 'zh-CN',
            followingToolbar: false,
            callbacks: {
                onImageUpload: function (files) {
                    sendFile(files[0], this);
                },
                onPaste: function (ne) {
                    const bufferText = ((ne.originalEvent || ne).clipboardData || window.clipboardData).getData('Text/plain');
                    ne.preventDefault ? ne.preventDefault() : (ne.returnValue = false);
                    document.execCommand("insertText", false, bufferText);
                }
            }
        });
        var content = $("#fullContent").val();
        $('#editor').summernote('code', content);
    });

    // 上传文件
    function sendFile(file, obj) {
        var data = new FormData();
        data.append("file", file);
        $.ajax({
            type: "POST",
            url: ctx + "common/upload",
            data: data,
            cache: false,
            contentType: false,
            processData: false,
            dataType: 'json',
            success: function (result) {
                if (result.status === web_status.SUCCESS) {
                    $(obj).summernote('editor.insertImage', result.body.url, result.body.fileName);
                } else {
                    $.modal.alertError(result.msg);
                }
            },
            error: function (error) {
                $.modal.alertWarning("图片上传失败。");
            }
        });
    }

    /* 亿图数学公式编辑*/
    function mathEdit() {
        $.modal.open("亿图片", "https://math.edrawsoft.cn/", '1200', '700');
    }

    /*所有课程列表*/
    function courseRadioTree() {
        var options = {
            title: '课程选择',
            width: "380",
            url: prefix + "/courseRadioTreeView",
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero) {
        var body = layer.getChildFrame('body', index);
        $("#treeId").val(body.find('#treeId').val());
        $("#treeName").val(body.find('#treeName').val());
        layer.close(index);
    }

    $("#form-question-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            var sHTML = $('.summernote').summernote('code');
            $("#fullContent").val(sHTML);
            $.operate.save(prefix + "/edit", $('#form-question-edit').serialize());
        }
    }
</script>
</body>
</html>
